home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / String.h < prev    next >
C/C++ Source or Header  |  1990-05-19  |  9KB  |  252 lines

  1. #ifndef    STRING_H
  2. #define    STRING_H
  3.  
  4. /*$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/String.h,v 3.0 90/05/20 00:21:38 kgorlen Rel $*/
  5.  
  6. /* String.h -- declarations for character strings
  7.  
  8.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  9.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  10.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  11.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  12.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  13.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  14.  
  15. Authors:
  16.     C. J. Eppich and K. E. Gorlen
  17.     Bg. 12A, Rm. 2033
  18.     Computer Systems Laboratory
  19.     Division of Computer Research and Technology
  20.     National Institutes of Health
  21.     Bethesda, Maryland 20892
  22.  
  23. $Log:    String.h,v $
  24.  * Revision 3.0  90/05/20  00:21:38  kgorlen
  25.  * Release for 1st edition.
  26.  * 
  27. */
  28.  
  29. #include "Object.h"
  30. #include <string.h>
  31. #include <malloc.h>
  32.  
  33. const unsigned DEFAULT_STRING_EXTRA = 15;
  34. class String;
  35. class Range;
  36.  
  37. class SubString : public NIHCL {
  38.     char* sp;        // substring pointer 
  39.         unsigned sl;        // substring length 
  40.     String* st;        // String this is a SubString of
  41.     SubString(const String&, unsigned, unsigned);
  42.     SubString(const SubString& ss) {
  43.         sp = ss.sp; sl = ss.sl; st = ss.st;
  44.     }
  45.     int compare(const char*) const;
  46.     int compare(const String&) const;
  47.     int compare(const SubString&) const;
  48.     void replace(const char* st, unsigned ln);
  49.     void checkSubStr() const;
  50.     friend String;
  51. public:
  52.     const char* ptr() const    { return sp; }
  53.     unsigned position() const;
  54.     unsigned length() const    { return sl; }
  55.  
  56.     void operator=(const String&);
  57.     void operator=(const SubString&);
  58.     void operator=(const char*);
  59.  
  60.     bool operator<(const String&) const;
  61.     bool operator>(const String&) const;
  62.     bool operator<=(const String&) const;
  63.     bool operator>=(const String&) const;
  64.     bool operator==(const String&) const; 
  65.     bool operator!=(const String&) const;
  66.  
  67.     bool operator<(const SubString& ss) const    { return compare(ss)<0; } 
  68.     bool operator>(const SubString& ss) const    { return compare(ss)>0; }
  69.     bool operator<=(const SubString& ss) const    { return compare(ss)<=0; }
  70.     bool operator>=(const SubString& ss) const    { return compare(ss)>=0; }
  71.     bool operator==(const SubString& ss) const    { return compare(ss)==0; }
  72.     bool operator!=(const SubString& ss) const    { return compare(ss)!=0; }
  73.  
  74.     bool operator<(const char* cs) const    { return compare(cs) < 0; }   
  75.     bool operator>(const char* cs) const    { return compare(cs) > 0; }   
  76.     bool operator<=(const char* cs) const    { return compare(cs) <= 0; }
  77.     bool operator>=(const char* cs) const    { return compare(cs) >= 0; }
  78.     bool operator==(const char* cs) const    { return compare(cs) == 0; }
  79.     bool operator!=(const char* cs) const    { return compare(cs) != 0; }
  80.  
  81.     friend bool operator<(const char* cs, const SubString& ss) {
  82.         return ss.compare(cs) > 0;
  83.     }
  84.     friend bool operator>(const char* cs, const SubString& ss) {     
  85.         return ss.compare(cs) < 0;
  86.     }
  87.     friend bool operator<=(const char* cs, const SubString& ss) { 
  88.         return ss.compare(cs) >= 0; 
  89.     }
  90.     friend bool operator>=(const char* cs, const SubString& ss) { 
  91.         return ss.compare(cs) <= 0;
  92.     }
  93.     friend bool operator==(const char* cs, const SubString& ss) {
  94.         return ss.compare(cs) == 0; 
  95.     }
  96.     friend bool operator!=(const char* cs, const SubString& ss) {
  97.         return ss.compare(cs) != 0; 
  98.     }
  99.  
  100.     String operator&(const String&) const;
  101.     String operator&(const SubString&) const;
  102.     String operator&(const char*) const;
  103.     friend String operator&(const char*, const SubString&);
  104.  
  105.     void dumpOn(ostream& strm =cerr) const;
  106.     void printOn(ostream& strm =cout) const;
  107. };
  108.  
  109. class String: public VIRTUAL Object {
  110.     DECLARE_MEMBERS(String);
  111.     char* p;        // character string
  112.     unsigned len;        // length of string, excluding null character
  113.     unsigned alloc;        // amount of storage allocated
  114.     void indexRangeErr() const;
  115.     friend SubString;
  116. protected:        // storer() functions for object I/O
  117.     virtual void storer(OIOofd&) const;
  118.     virtual void storer(OIOout&) const;        
  119. public:
  120.     String(const char& c, unsigned l=1, unsigned extra=DEFAULT_STRING_EXTRA);
  121.     String();
  122.     String(unsigned extra);
  123.     String(const char*);
  124.     String(const char*, unsigned extra);
  125.     String(const String&);
  126.     String(const String&, unsigned extra);
  127.     String(const SubString&);
  128.     String(const SubString&, unsigned extra);
  129.     ~String();
  130.     operator const char*() const    { return p; }
  131.     SubString operator()(unsigned pos, unsigned lgt);
  132.     const SubString operator()(unsigned pos, unsigned lgt) const;
  133.     SubString operator()(const Range& r);
  134.     const SubString operator()(const Range& r) const;
  135.     char& operator[](unsigned i) {
  136.         if (i >= len) indexRangeErr();
  137.         return p[i];
  138.         }
  139.     char operator[](unsigned i) const {
  140.         if (i >= len) indexRangeErr();
  141.         return p[i];
  142.         }
  143.  
  144.     void operator=(const String&);
  145.     void operator=(const SubString&);
  146.     void operator=(const char*);
  147.  
  148.     bool operator<(const String& s) const    { return strcmp(p, s.p) < 0; }
  149.     bool operator>(const String& s) const    { return strcmp(p, s.p) > 0; }
  150.     bool operator<=(const String& s) const    { return strcmp(p, s.p) <= 0; }
  151.     bool operator>=(const String& s) const    { return strcmp(p, s.p) >= 0; }
  152.     bool operator==(const String& s) const;
  153.     bool operator!=(const String& s) const    { return !(*this==s); }
  154.  
  155.     bool operator<(const SubString& ss) const     { return ss.compare(*this) > 0; }
  156.     bool operator>(const SubString& ss) const    { return ss.compare(*this) < 0; }
  157.     bool operator<=(const SubString& ss) const    { return ss.compare(*this) >= 0; }
  158.     bool operator>=(const SubString& ss) const    { return ss.compare(*this) <= 0; }
  159.     bool operator==(const SubString& ss) const    { return ss.compare(*this) == 0; }
  160.     bool operator!=(const SubString& ss) const    { return ss.compare(*this) != 0; }
  161.  
  162.     bool operator<(const char* cs) const    { return strcmp(p,cs) < 0; }   
  163.     bool operator>(const char* cs) const    { return strcmp(p,cs) > 0; }   
  164.     bool operator<=(const char* cs) const    { return strcmp(p,cs) <= 0; }
  165.     bool operator>=(const char* cs) const    { return strcmp(p,cs) >= 0; }
  166.     bool operator==(const char* cs) const    { return strcmp(p,cs) == 0; }
  167.     bool operator!=(const char* cs) const    { return strcmp(p,cs) != 0; }
  168.  
  169.     friend bool operator<(const char* cs, const String& s) {
  170.         return strcmp(cs, s.p) < 0;
  171.     }
  172.     friend bool operator>(const char* cs, const String& s) {     
  173.         return strcmp(cs, s.p) > 0;
  174.     }
  175.     friend bool operator<=(const char* cs, const String& s) { 
  176.         return strcmp(cs, s.p) <= 0; 
  177.     }
  178.     friend bool operator>=(const char* cs, const String& s) { 
  179.         return strcmp(cs, s.p) >= 0;
  180.     }
  181.     friend bool operator==(const char* cs, const String& s) {
  182.         return strcmp(cs, s.p) == 0; 
  183.     }
  184.     friend bool operator!=(const char* cs, const String& s) {
  185.         return strcmp(cs, s.p) != 0; 
  186.     }
  187.  
  188.     String operator&(const String& s) const;
  189.     String operator&(const SubString& ss) const;
  190.     String operator&(const char* cs) const;
  191.     friend String operator&(const char*, const SubString&);
  192.     friend String operator&(const char* cs, const String& s);
  193.  
  194.     String& operator&=(const String&);
  195.     String& operator&=(const SubString&);
  196.     String& operator&=(const char* cs);
  197.  
  198.     char& at(unsigned i)            { return (*this)[i]; }
  199.     char at(unsigned i) const        { return (*this)[i]; }
  200.     unsigned length() const    { return len; } 
  201.     unsigned reSize(unsigned new_capacity);
  202.  
  203.     virtual void toAscii();
  204.     virtual void toLower();
  205.     virtual void toUpper();
  206.     virtual unsigned capacity() const;
  207.     virtual int compare(const Object& ob) const;
  208.     virtual void deepenShallowCopy();
  209.     virtual unsigned hash() const;
  210.     virtual bool isEqual(const Object&) const;
  211.     virtual void printOn(ostream& strm =cout) const;
  212.     virtual void scanFrom(istream& strm);
  213.     virtual unsigned size() const;
  214.     virtual const Class* species() const;
  215. };
  216.  
  217. inline ostream& operator<<(ostream& strm, const SubString& ss)
  218. {
  219.     ss.printOn(strm);
  220.     return(strm);
  221. }
  222.  
  223. inline SubString::SubString(const String& s, unsigned pos, unsigned lgt)
  224.     sp = &((String&)s).p[pos];
  225.     sl = lgt;
  226.     st = &(String&)s;
  227.     checkSubStr();
  228. }
  229.     
  230. inline unsigned SubString::position() const    { return sp - st->p; }
  231.  
  232. inline SubString String::operator()(unsigned pos, unsigned lgt)
  233. {    
  234.     return SubString(*this, pos, lgt);
  235. }
  236.  
  237. inline const SubString String::operator()(unsigned pos, unsigned lgt) const
  238. {    
  239.     return SubString(*this, pos, lgt);
  240. }
  241.  
  242. inline bool SubString::operator<(const String& s) const { return compare(s) < 0; }
  243. inline bool SubString::operator>(const String& s) const { return compare(s) > 0; }
  244. inline bool SubString::operator<=(const String& s) const { return compare(s) <= 0; }
  245. inline bool SubString::operator>=(const String& s) const { return compare(s) >= 0; }
  246. inline bool SubString::operator==(const String& s) const { return compare(s) == 0; }
  247. inline bool SubString::operator!=(const String& s) const { return compare(s) != 0; }
  248.  
  249. #endif
  250.  
  251.